//
//  MineMoveCardViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/9/20.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MineMoveCardViewController.h"
#import "MineMoveCardBtnV.h"

#define CELL_LEFT (50 * __MAIN_RATIO_375)
#define Cell_ID @"cellID"
@interface MineMoveCardViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic , strong) UIView * headGrayV;

@property (nonatomic , strong) NSMutableDictionary * cellDic;


@property (nonatomic , strong) BaseViewModel <MineMoveCardViewModelDelegate> * viewModel;
@end

@implementation MineMoveCardViewController
- (instancetype)initWithViewModel:(BaseViewModel<MineMoveCardViewModelDelegate> *)viewModel
{
    self = [super initWithMainViewType:TYPE_MAIN_VIEW_NORMAL];
    if (self) {
        self.viewModel = viewModel;
        self.viewModel.viewController = self;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Theme backGroundColor];
    self.title = @"我的挪车卡";
    
    __weak __typeof(self) wself = self;
    [self navAddRightImageName:@"moveCarSetting" action:^{
        [wself.viewModel clickOnNavSetting];
    }];
    [self navAddRightImageName:@"moveCarAdd" action:^{
        [wself.viewModel clickOnNavAdd];
    }];
    
    [self.view addSubview:self.headGrayV];
    
    [self navResetTitleColor:COLOR_HEXSTRING(@"#a3a3a3") font:FONT_DEFAULT_Light(17)];
    
    [self.viewModel myCarGetSelectIndex:0];
    self.cellDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [self createUI];

}
- (void)navClickOnBackItem
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.viewModel myCarWillAppear];
}

/**
 *  是否能右滑返回手势
 */
-(BOOL)viewControllerShouldGesturePopBack
{
    return NO;
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

- (void)createUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout * collectionLayout = [[UICollectionViewFlowLayout alloc]init];
    collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20* __MAIN_RATIO_375, __MAIN_WIDTH, CELL_HEIGHT_COLLECTION + 1) collectionViewLayout:collectionLayout];
    self.collectionV.backgroundColor = [Theme backGroundColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.allowsMultipleSelection = NO;
    self.collectionV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionV];
    [self.collectionV registerClass:[MineMoveCarCollectCell class] forCellWithReuseIdentifier:Cell_ID];
    
    __weak __typeof(self) wself = self;
    RACSignal * offsetValidSignal = RACObserve(self.collectionV, contentOffset);
    [offsetValidSignal subscribeNext:^(id x) {
        [wself observeCollectionViewOffset];
    }];
    [self.collectionV selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.collectionV.bottom + 7 * __MAIN_RATIO_375, __MAIN_WIDTH, 10)];
    self.pageControl.backgroundColor = [UIColor clearColor];
    self.pageControl.currentPageIndicatorTintColor = [Theme mainNavColor];
    self.pageControl.pageIndicatorTintColor = [Theme wordGrayColor];
    self.pageControl.enabled = NO;
    self.pageControl.numberOfPages = [self.viewModel myCardCollectionCellCount];
    [self.view addSubview:self.pageControl];
    [self.pageControl setHidden:[self.viewModel myCardCollectionCellCount] < 2];
//    self.pageControl.backgroundColor = [UIColor clearColor];
    
    MineMoveCardBtnV * btnV = [[[MineMoveCardBtnV alloc] initWithOrigin:CGPointMake(0, __MAIN_HEIGHT_64 - Bottom_BtnV_Height)] setupOnView:self.view];
    btnV.btnClick = ^(NSInteger index)
    {
        [wself.viewModel clickOnDeleteOrChangeAndSoOn:index];
    };
    btnV.moveCarTipClick = ^()
    {
        [wself.viewModel moveCarTipsClick];
    };
}
- (void)observeCollectionViewOffset
{
    CGFloat oldPage = self.pageControl.currentPage;
    NSInteger nowIndex = (self.collectionV.contentOffset.x + CELL_LEFT) / CELL_WIDTH_COLLECTION;
    self.pageControl.currentPage = nowIndex;
    [self.viewModel myCarGetSelectIndex:nowIndex];
    if(oldPage != nowIndex && (nowIndex >= 0 && nowIndex < [self.viewModel myCardCollectionCellCount]))[self.collectionV reloadData];
}

#pragma mark ---- UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.viewModel myCardCollectionCellCount];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MineMoveCarCollectCell * cell = [collectionView  dequeueReusableCellWithReuseIdentifier:Cell_ID forIndexPath:indexPath];
    [cell reloadDataWithModel:[self.viewModel myCardGetCollectionModelIndex:indexPath.section]];
    [cell isCurrentCell:(self.selectMyCarIndex == indexPath.section)];
    
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return (CGSize){CELL_WIDTH_COLLECTION, CELL_HEIGHT_COLLECTION};
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, (section == 0 ? CELL_LEFT: 0), 0, ((section == [self.viewModel myCardCollectionCellCount] - 1) ? CELL_LEFT : 0));
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UIView *)headGrayV
{
    if (!_headGrayV) {
        _headGrayV = [[UIView viewWithFrame:CGRectMake(0, -64, __MAIN_WIDTH, 70)] resetBackgroundColor:[Theme backGroundColor]];
    }
    return _headGrayV;
}
#pragma mark - nav
-(UIColor *)navBackgroundColor{
    return [UIColor clearColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
